//var Hobbies = mongoose.model('masterData_Hobbies');
var Regions = mongoose.model('masterData_Regions');
var Dictionary = mongoose.model('masterData_Dictionaries');
var Users = mongoose.model('user');
var userPortrait = mongoose.model('masterData_UserPortrait');


////获取兴趣列表
//exports.getHobbyListService = function (callback) {
//    Hobbies.find({}, '_id hobbyName').exec(callback);
//};

//获取国家列表
exports.getCountryListService = function (callback) {
    Regions.find({'type': 'country'}, '_id name areaCode').exec(callback);
};

//获取某个国家下所有省
exports.getProvinceListByCountyIdService = function (countryId, callback) {
    Regions.find({'type': 'province', 'parentId': countryId}, '_id name areaCode').exec(callback);
};

//获取某个省下所有市
exports.getCityListByProvinceIdService = function (provinceId, callback) {
    Regions.find({'type': 'city', 'parentId': provinceId}, '_id name areaCode').exec(callback);
};

//获取某个市下所有县
exports.getCountyListByCityIdService = function (cityId, callback) {
    Regions.find({'type': 'county', 'parentId': cityId}, '_id name areaCode').exec(callback);
};

//获取某个国家、省、市、县的信息
exports.getRegionByIdService = function (regionId, callback) {
    Regions.findOne({'_id': regionId}, '_id name areaCode type cultureCode').exec(callback);
};

//获取地区列表
exports.getRegionListService = function (date, callback) {
    Regions.findOne({}, {}, function (err, results) {
        if (results != null && results._doc.dateLastModified > new Date(date)) {
            Regions.find({}, '_id type name parentId cultureCode areaCode dateLastModified').exec(callback);
        } else {
            callback(null, null);
        }
    });
}

//获取数据字典
exports.getDataDictionaryService = function (date, callback) {
    Dictionary.findOne({}, {}, function (err, results) {
        if (results._doc.dateLastModified > new Date(date)) {
            Dictionary.find({
                $or: [{'category': {$ne: '02'}}, {
                    'category': '02',
                    'displayOrder': {$ne: null}
                }]
            }, '_id category value defaultURL imageURL dateLastModified').sort('displayOrder').exec(function (err, result) {
                callback(err, result);
            });
        } else {
            callback(null, null);
        }
    });
};

//根据类别获取数据字典
exports.getDataDictionaryByCategoryService = function (category, callback) {
    Dictionary.find({category: category, $and: [{value: {$ne: '全部'}}, {value: {$ne: '推荐'}}]}).sort('displayOrder')
        .exec(function (err, results) {
            if (results == undefined || results == null) {
                var msg = {
                    "message": 'get err'
                };
                callback(msg, null);
            } else {
                callback(null, results);
            }
        })
};

exports.getUserPortraitService = function (date, callback) {
    userPortrait.findOne({}, {}, function (err, results) {
        if (results._doc.dateCreated > new Date(date)) {
            userPortrait.find({}, '_id value portraitURL gender dateCreated').sort('displayOrder').exec(callback);
        } else {
            callback(null, null);
        }
    });
};

